import SelectGroupsBase from "../../components/select-groups-base/index.vue";
import SelectGroupsTag from "../../components/select-groups-tag/index.vue";
import questionApi from "@rc/apis/rc/question.mjs";

export default {
	props: {
		// 是否显示弹窗,可通过.sync进行绑定
		visibly: {
			type: Boolean,
			default: false
		},
		// 当前编辑的试题所属类型
		editQuestionType: {
			type: String,
			default: "normal"
		},
		subjectId: {
			//科目ID
			type: String,
			required: true
		},
		subjectName: {
			//科目名称
			type: String,
			default: ""
		},
		projectName: {
			//项目名称
			type: String,
			default: ""
		},
		// 编辑的试题信息
		defaultData: {
			type: Object,
			default: () => {}
		}
	},
	components: {
		SelectGroupsBase,
		SelectGroupsTag
	},
	data() {
		return {
			isLoading: false,
			isShow: this.visibly, // 内部的是否显示弹窗
			basicInfo: {}, // 默认显示的基础信息
			tagInfo: {} // 默认显示的标签信息
		};
	},
	watch: {
		// 内部和外部的变量双向绑定
		isShow(val) {
			this.$emit("update:visibly", val);
		},
		visibly(val) {
			// 切换显示状态时，初始化默认选中内容
			if (val) {
				const copyData = JSON.parse(JSON.stringify(this.defaultData));
				if (this.editQuestionType !== "parent") {
					this.basicInfo = {
						subjectId: this.subjectId,
						chapterList: copyData.questionChapterResponseVoList,
						chapterIds: copyData.questionChapterResponseVoList.map((i) => i.chapterId),
						knowledgeList: copyData.questionKnowledgeResponseVoList,
						questionDeveloperId: copyData.questionDeveloperId
					};
				}
				this.tagInfo = {
					applicableYear: copyData.applicableYear,
					areaIds: copyData.areaRelationListResponseVoList.map((i) => i.cityId),
					labelList: copyData.labelList
				};
			}
			this.isShow = val;
		}
	},
	methods: {
		//点击确定按钮
		onConfirm() {
			const { applicableYear, areaList, labelList } = this.$refs.tagRef.api_result();

			let params = {};
			if (this.editQuestionType === "parent") {
				// 父题
				params = {
					resourceId: this.defaultData.resourceId, // 资源标识
					applicableYear, // 年份
					areaList, // 地区列表
					labelList: labelList.filter((i) => ["试题分类", "适用范围"].includes(i.labelName)) // 标签列表
				};
			} else if (this.editQuestionType === "child") {
				// 子题
                const baseInfo = this.$refs.baseRef.api_result();
				params = {
					resourceId: this.defaultData.resourceId, // 资源标识
					chapterList: this.$refs.baseRef.$refs.chapterCascader.api_getChapterList(), // 章节列表
					knowledgeList: baseInfo.knowledgeList, // 知识点列表
					labelList: labelList.filter((i) => !["试题分类", "适用范围"].includes(i.labelName)), // 标签列表
                    questionDeveloperId: baseInfo.questionDeveloperId, // 研发人id
					questionDeveloperName: baseInfo.questionDeveloperName // 研发人名称
				};
			} else {
				// 普通题
				const baseInfo = this.$refs.baseRef.api_result();
				params = {
					resourceId: this.defaultData.resourceId, // 资源标识
					applicableYear, // 年份
					areaList, // 地区列表
					chapterList: this.$refs.baseRef.$refs.chapterCascader.api_getChapterList(), // 章节列表
					knowledgeList: baseInfo.knowledgeList, // 知识点列表
					labelList: labelList, // 标签列表
					questionDeveloperId: baseInfo.questionDeveloperId, // 研发人id
					questionDeveloperName: baseInfo.questionDeveloperName // 研发人名称
				};
			}
			this.isLoading = true;
			questionApi.updateQuestionSupplementary({
				data: params,
				success: () => {
					this.$message.success("保存成功");
					if (this.editQuestionType !== "parent") {
						// 后端只需要末级章节名称，但是前端展示需要完整章节链，进行拼接
						params.chapterList = this.$refs.baseRef.$refs.chapterCascader.api_getChapterListFullName();
					}
					this.$emit("success", params, this.defaultData);
					this.isShow = false;
				},
				complete: () => {
					this.isLoading = false;
				}
			});
		}
	}
};
